define(['static', '../../../bean/user', 'confirm'], function(CONST, userModel) {
    extend(Main, Combine);

    function Main() {
        var _this = this;
        var _super = arguments.callee.parent;
        this.init = function() {
            _this._DOM = $("[data-com='login']");
            userModel.queryUser(function(user) {
                var userMenu = [{
                    label: I18N.getText('index', 'editUser'),
                    href: "#!profile",
                    class:'editUser',
                    val: 'editUser'
                }, {
                    label: I18N.getText('index', 'editPassword'),
                    href: "#!chgpwd",
                    class:'editPassword',
                    val: 'editPassword',
                    split:true
                }];
                // 美宣、外部美宣不显示 用户中心-解决绑定
                if (userModel.isUidesigner() || userModel.isOuteruidesigner()) {

                }
                userMenu.push({
                    label: I18N.getText('index', 'logout'),
                    href: "javascript:;",
                    class: 'btn-logout',
                    val: 'logout'
                });
                _this._DOM.drop2({
                    id: "meme",
                    caret: "<i class='glyphicon glyphicon-menu-down'></i>",
                    label: user.username,
                    data: userMenu
                });
                _this.event();
            });
        };

        this.event = function() {
            this._DOM.off('click').on('click', function(e) {
                e.stopPropagation();
                $('#mess-drop').removeClass('active');
                _this._DOM.toggleClass('open');
                _this._DOM.off('click', '.btn-logout').on('click', '.btn-logout', $.proxy(_this.logout, _this));
                _this._DOM.off('click', '.editPassword').on('click', '.editPassword', $.proxy(function(){
                    window.location.href = 'index.html#!chgpwd';
                }, _this));
                _this._DOM.off('click', '.editUser').on('click', '.editUser', $.proxy(function(){
                    window.location.href = 'index.html#!profile';
                }, _this));
            });
        };

        this.logout = function(e) {
            $("#confirm-holder").confirm({
                title: I18N.getText('index', 'alert'),
                content: I18N.getText('index', 'logoutMsg'),
                btnOK: I18N.getText('index', 'logout'),
                btnCANCEL: I18N.getText('index', 'cancel')
            }).off('click', '.btn-ok').on('click', '.btn-ok', function(e) {
                userModel.logout(function(result) {
                    var sl = window.localStorage.getItem('language');
                    var rememberMeObj = window.localStorage.getItem('rememberMe');
                    var reportTitle = window.localStorage.getItem('report-list-22');
                    localStorage.clear();
                    window.localStorage.setItem('language', sl);
                    window.localStorage.setItem('rememberMe', rememberMeObj);
                    window.localStorage.setItem('report-list-22', reportTitle);
                    sessionStorage.clear();
                    window.top.location.hash="";
                    window.location.href = 'login.html';
                }, function() {
                    var sl = window.localStorage.getItem('language');
                    var rememberMeObj = window.localStorage.getItem('rememberMe');
                    var reportTitle = window.localStorage.getItem('report-list-22');
                    localStorage.clear();
                    window.localStorage.setItem('language', sl);
                    window.localStorage.setItem('rememberMe', rememberMeObj);
                    window.localStorage.setItem('report-list-22', reportTitle);
                    sessionStorage.clear();
                    window.top.location.hash="";
                    window.location.href = 'login.html';
                });
            })
        }
    }
    return new Main();
});
